package com.chen.controller;

import com.chen.domain.SysMenu;
import com.chen.dto.R;
import com.chen.model.RolePrivilegesParam;
import com.chen.service.SysMenuService;
import com.chen.service.SysRolePrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: chen
 * @Date: 2021/4/20 2:46 PM
 * @Version 1.0
 */
@RestController
@Api(tags = "角色权限配置")
public class SysRolePrivilegeController {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;


    //查询角色权限配置列表
    //GET/role_privileges
    //分页查询权限配置列表
    @GetMapping("/roles_privileges")
    @ApiOperation(value = "查询角色权限配置列表")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "roleId", value = "角色id")
            }
    )
//    @PreAuthorize("hasAuthority('sys_privilege_query')")
    public R<List<SysMenu>> findRolePrivileges(
            @RequestParam("roleId") String roleId) {
        List<SysMenu> sysMenus = sysMenuService.findRolePrivileges(roleId);
//        List<SysMenu> sysMenus  = sysRolePrivilegeService.findSysMenuAndPrivileges(roleId) ;
        return R.ok(sysMenus);
    }

    //授予角色权限
    //POST/grant_privileges
    @PostMapping("/grant_privileges")
    @ApiOperation(value = "授予角色权限")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "privilegeIds", value = "权限的id数组"),
                    @ApiImplicitParam(name = "roleId", value = "角色id")
            }
    )
    public R grant(@RequestBody RolePrivilegesParam rolePrivilegesParam) {
        sysMenuService.grantPrivileges(rolePrivilegesParam);
        return R.ok();
    }

}



